package cn.wbu.zyh.controller.user;

import cn.wbu.zyh.dto.InvitationDTO;
import cn.wbu.zyh.dto.TeamMemberDTO;
import cn.wbu.zyh.entity.Invitation;
import cn.wbu.zyh.entity.TeamMember;
import cn.wbu.zyh.result.Result;
import cn.wbu.zyh.service.InvitationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/invite")
@Slf4j
public class InvitationController {
    @Autowired
    private InvitationService invitationService;

    @PostMapping("/teamInvites")
    public Result inviteUser(@RequestBody InvitationDTO invitationDTO) {
        invitationService.createInvite(invitationDTO);
        return Result.success();  // 返回简单消息，前端根据需要处理
    }

    @GetMapping("/pending")
    public Result getPendingInvites(@RequestParam String username) {
        List<Invitation> pendingInvites = invitationService.getPendingInvites(username);
        return Result.success(pendingInvites);
    }
    @GetMapping("/history")
    public Result getInviteHistory(@RequestParam String username) {
        List<Invitation> inviteHistory = invitationService.getInviteHistory(username);
        return Result.success(inviteHistory);
    }
    @PostMapping("/accept")
    public Result<String> acceptInvite(@RequestBody TeamMemberDTO teamMemberDTO) {
        invitationService.acceptInvite(teamMemberDTO);
        return Result.success("邀请已接受");
    }

    // 拒绝邀请（前端以 JSON 格式传递 { "inviteId": ... }）
    @PostMapping("/reject")
    public Result<String> rejectInvite(@RequestBody Map<String, Object> requestData) {
        String inviteId = (String) requestData.get("inviteId");
        invitationService.rejectInvite(inviteId);
        return Result.success("邀请已拒绝");
    }
}
